package com.ruoyi.patrol.service.impl;

import java.util.List;
import java.util.UUID;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.patrol.domain.PtNotificationUser;
import com.ruoyi.patrol.mapper.PtNotificationUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.patrol.mapper.PtNotificationMapper;
import com.ruoyi.patrol.domain.PtNotification;
import com.ruoyi.patrol.service.IPtNotificationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通知公告Service业务层处理
 *
 * @author 理想三旬
 * @date 2022-11-06
 */
@Service
public class PtNotificationServiceImpl implements IPtNotificationService {

    @Autowired
    private PtNotificationMapper ptNotificationMapper;

    @Autowired
    private PtNotificationUserMapper ptNotificationUserMapper;

    /**
     * 查询通知公告
     *
     * @param id 通知公告ID
     * @return 通知公告
     */
    @Override
    public PtNotification selectPtNotificationById(String id) {
        return ptNotificationMapper.selectPtNotificationById(id);
    }

    /**
     * 查询通知公告列表
     *
     * @param ptNotification 通知公告
     * @return 通知公告
     */
    @Override
    public List<PtNotification> selectPtNotificationList(PtNotification ptNotification) {
        return ptNotificationMapper.selectPtNotificationList(ptNotification);
    }

    /**
     * 新增通知公告
     *
     * @param ptNotification 通知公告
     * @return 结果
     */

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertPtNotification(PtNotification ptNotification) {
        ptNotification.setCreateTime(DateUtils.getNowDate());
        String uuid = UUID.randomUUID().toString();
        ptNotification.setId(uuid);
        ptNotificationMapper.insertPtNotification(ptNotification);
        return ptNotificationUserMapper.insertPtNotificationUserList(ptNotification.getSchoolsKey(), uuid, SecurityUtils.getUsername());
    }

    /**
     * 修改通知公告
     *
     * @param ptNotification 通知公告
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updatePtNotification(PtNotification ptNotification) {
        ptNotification.setUpdateTime(DateUtils.getNowDate());
        ptNotificationUserMapper.deletePtNotificationUserById(ptNotification.getId());
        ptNotificationMapper.updatePtNotification(ptNotification);
        return ptNotificationUserMapper.insertPtNotificationUserList(ptNotification.getSchoolsKey(), ptNotification.getId(), SecurityUtils.getUsername());
    }

    /**
     * 批量删除通知公告
     *
     * @param ids 需要删除的通知公告ID
     * @return 结果
     */
    @Override
    public int deletePtNotificationByIds(String[] ids, int type) {
        return ptNotificationMapper.deletePtNotificationByIds(ids, type);
    }

    /**
     * 删除通知公告信息
     *
     * @param id 通知公告ID
     * @return 结果
     */
    @Override
    public int deletePtNotificationById(String id) {
        return ptNotificationMapper.deletePtNotificationById(id);
    }

    @Override
    public List<PtNotification> selectPtNotificationListNoPage() {
        return ptNotificationMapper.selectPtNotificationListNoPage();
    }
}
